//
//  TextInputViewController.swift
//  aj
//
//  Created by leeqb on 1/29/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class TextInputViewController: AJBaseViewController, UITextFieldDelegate {
    var completeBlock: ((text: String) -> Void)!
    @IBOutlet weak var textInputView: TextInputView!
    @IBOutlet weak var finishButton: CBButton!

    var text: String! {
        didSet {
            if textInputView != nil {
                textInputView.textField.text = text
            }
        }
    }
    
    var placeHolder: String! {
        didSet {
            if textInputView != nil {
                textInputView.textField.placeholder = placeHolder
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        textInputView.textField.text = text
        textInputView.textField.placeholder = placeHolder
        textInputView.textField.delegate = self
    }
    
    //MARK: - UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        inputFinished(finishButton)
        return true
    }

    //MARK: - Button Clicks
    @IBAction func inputFinished(sender: CBButton) {
        if completeBlock != nil {
            let text = textInputView.textField.text!
            if !text.isEmpty {
                completeBlock(text: text)
            }
        }
        self.navigationController?.popViewControllerAnimated(true)
    }
}
